package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.BywhShop;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.BywhShopRepository;
import co.yixiang.modules.shop.service.BywhShopService;
import co.yixiang.modules.shop.service.dto.BywhShopDto;
import co.yixiang.modules.shop.service.dto.BywhShopQueryCriteria;
import co.yixiang.modules.shop.service.mapper.BywhShopMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-10
*/
@Service
//@CacheConfig(cacheNames = "bywhShop")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BywhShopServiceImpl implements BywhShopService {

    private final BywhShopRepository bywhShopRepository;

    private final BywhShopMapper bywhShopMapper;

    public BywhShopServiceImpl(BywhShopRepository bywhShopRepository, BywhShopMapper bywhShopMapper) {
        this.bywhShopRepository = bywhShopRepository;
        this.bywhShopMapper = bywhShopMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(BywhShopQueryCriteria criteria, Pageable pageable){
        Page<BywhShop> page = bywhShopRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<BywhShopDto> map = page.map(bywhShopMapper::toDto);
        return PageUtil.toPage(map);
    }

    @Override
    //@Cacheable
    public List<BywhShopDto> queryAll(BywhShopQueryCriteria criteria){
        return bywhShopMapper.toDto(bywhShopRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public BywhShopDto findById(Integer id) {
        BywhShop bywhShop = bywhShopRepository.findById(id).orElseGet(BywhShop::new);
        ValidationUtil.isNull(bywhShop.getId(),"BywhShop","id",id);
        return bywhShopMapper.toDto(bywhShop);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public BywhShopDto create(BywhShop resources) {
        return bywhShopMapper.toDto(bywhShopRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(BywhShop resources) {
        BywhShop bywhShop = bywhShopRepository.findById(resources.getId()).orElseGet(BywhShop::new);
        ValidationUtil.isNull( bywhShop.getId(),"BywhShop","id",resources.getId());
        bywhShop.copy(resources);
        bywhShopRepository.save(bywhShop);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            bywhShopRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<BywhShopDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BywhShopDto bywhShop : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("商户id", bywhShop.getMerId());
            map.put("名称", bywhShop.getName());
            map.put("联系方式", bywhShop.getPhone());
            map.put("图片", bywhShop.getImage());
            map.put("范围", bywhShop.getRange());
            map.put("描述", bywhShop.getDescription());
            map.put("省code", bywhShop.getProvinceCode());
            map.put("省", bywhShop.getProvince());
            map.put("市code", bywhShop.getCityCode());
            map.put("市", bywhShop.getCity());
            map.put("区code", bywhShop.getDistrictCode());
            map.put("区", bywhShop.getDistrict());
            map.put("创建时间", bywhShop.getCreateTime());
            map.put("是否删除", bywhShop.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}